<template>
<div class="box-padding">
    <div class="role-title">{{ $route.params.name }}</div>
    <Tabs value="name1">
        <TabPane label="基本信息" name="name1">
            <Basic :obj="obj"></Basic>
        </TabPane>
        <TabPane label="工商信息" name="name2">
            <Work :obj="obj"></Work>
        </TabPane>
        <TabPane label="证件信息" name="name3">
            <Card :obj="obj"></Card>
        </TabPane>
    </Tabs>
    <div class="role-bottom" v-show="$route.params.type != 'detail'">
        <Button type="primary" @click="save">保存</Button>
        <Button style="margin-left:20px;" @click="cancel">取消</Button>
    </div>
</div>
</template>

<script>
import {
    mapMutations
} from 'vuex'
import closeTag from '../../../mixins/closeTag'
import Basic from './CompanyTab/Basic' // 基本
import Work from './CompanyTab/Work' // 工商
import Card from './CompanyTab/Card' // 证件
export default {
    mixins: [closeTag],
    components: {
        Basic,
        Work,
        Card
    },
    data() {
        return {
            obj: {
                code:'',
                qualificationList: [],
                licenseList: [],
            }
        }
    },
    watch: {
        // $route(to, from) {
        //     if (to.name == 'add-company') {
        //         if (this.$route.params.type != 'add') {
        //             this.enterpriseDetail()
        //         } else {
        //             this.obj = {}
        //         }
        //     }
        // }
    },
    mounted() {
        if (this.$route.params.type != 'add') {
            this.enterpriseDetail();
        }
    },
    computed: {
        currType() {
            return this.$route.params.type;
        }
    },
    methods: {
        ...mapMutations(['closeTag']),
        closetab() {
            this.closeTag({
                name: this.$route.name,
                params: this.$route.params,
                query: this.$route.query
            });
        },
        //保存
        save() {
            
            this.obj.type = 1;
            this.scopeList = [];

            if (this.obj.code.length > 30) {
                this.$Message.warning("机构编号不能超过30个字符！");
                return;
            }
            if (!this.$regex.numberAndEnglishRegEx(this.obj.code)) {
                this.$Message.warning("机构编号只能包含英文字母和数字！");
                return;
            }
            if (!this.obj.name) {
                this.$Message.warning("机构全称不能为空！");
                return;
            }
            if (this.obj.name.length > 60) {
                this.$Message.warning("机构全称不能超过60个字符！");
                return;
            }
            if (!this.obj.shortName) {
                this.$Message.warning("机构简称不能为空！");
                return;
            }
            if (this.obj.shortName.length > 60) {
                this.$Message.warning("机构简称不能超过60个字符！");
                return;
            }
            if (!this.obj.provinceId) {
                this.$Message.warning("所在地区不能为空");
                return;
            }
            if (!this.obj.address) {
                this.$Message.warning("办公地址不能为空！");
                return;
            }
            if (this.obj.address.length > 60) {
                this.$Message.warning("办公地址不能超过60个字符！");
                return;
            }
            if (!this.obj.linkman) {
                this.$Message.warning("联系人不能为空！");
                return;
            }
            if (this.obj.linkman.length > 20) {
                this.$Message.warning("联系人不能超过20个字符！");
                return;
            }
            if (!this.obj.linkmanPhone) {
                this.$Message.warning("联系电话不能为空！");
                return;
            }
            if (this.obj.linkmanPhone.length > 11) {
                this.$Message.warning("联系电话不能超过11个字符！");
                return;
            }
            if (!this.$regex.telRegEx(this.obj.linkmanPhone)) {
                this.$Message.warning('联系电话格式不正确')
                return;
            }
            if (this.obj.description && this.obj.description.length > 500) {
                this.$Message.warning("概况说明不能超过500个字符！");
                return;
            }
            if (this.obj.representative && this.obj.representative.length > 20) {
                this.$Message.warning('法定代表人不能超过20个字符！')
                return;
            }
            if (this.obj.cardId && !this.$regex.idCardRegEx(this.obj.cardId)) {
                this.$Message.warning('身份证号格式不正确！')
                return;
            }
            if (this.obj.telephone && this.obj.telephone.length > 11) {
                this.$Message.warning("联系方式不能超过11个字符！");
                return;
            }
            if (this.obj.telephone && !this.$regex.telRegEx(this.obj.telephone)) {
                this.$Message.warning('联系方式格式不正确')
                return;
            }
            if (this.obj.registerAddress && this.obj.registerAddress.length > 60) {
                this.$Message.warning("注册地址不能超过60个字符！");
                return;
            }
            if (this.obj.registerCapital && this.obj.registerCapital.length > 10) {
                this.$Message.warning("注册资本不能超过10个字符！");
                return;
            }
            if (this.obj.registerCapital && !this.$regex.moneyReg(this.obj.registerCapital)) {
                this.$Message.warning("注册资本只能包含数字和小数点！");
                return;
            }
            if (this.obj.scope && this.obj.scope.length > 500) {
                this.$Message.warning("经营范围不能超过500个字符！");
                return;
            }
            //处理经营期限
            if (!this.obj.deadLine) {
                this.obj.deadLine = ''
            }
            console.log(this.obj)
            if (this.$route.params.type == 'edit') {
                this.obj.parentId = this.$route.params.parentId;
                this.$api.companyOrganization.enterpriseEdit(this.obj).then(resp => {
                    if (resp.code == 200) {
                        this.$Message.success(resp.message);
                        this.closetab()
                        this.$router.push({
                            path: '/organization/enterprise-info'
                        })
                    } else {
                        this.$Message.warning(resp.data ? resp.data : resp.message);
                    }
                })
            } else {
                this.obj.parentId = this.$route.params.id;
                this.$api.companyOrganization.enterpriseAdd(this.obj).then(resp => {
                    if (resp.code == 200) {
                        this.$Message.success(resp.message);
                        this.closetab()
                        this.$router.push({
                            path: '/organization/enterprise-info'
                        })
                    } else {
                        this.$Message.warning(resp.data ? resp.data : resp.message);
                    }
                })
            }
        },
        // 取消
        cancel() {
            this.closetab()
            this.$router.push({
                path: '/organization/enterprise-info'
            })
        },
        // 获取详信息
        enterpriseDetail() {
            if (this.$route.params.name === '日志详情') {
                this.$api.operationLog.getOperationLogDetail(this.$route.params.objectId)
                    .then(res => {
                        if (res.code === 200) {
                            let data = JSON.parse(res.data.params)
                            data.deadLine = this.$time(
                                new Date(data.deadLine),
                                'yyyy-MM-dd'
                            )
                            data.qualificationList.forEach(e => {
                                e.deadLine = this.$time(
                                    new Date(e.deadLine),
                                    'yyyy-MM-dd'
                                )
                            })
                            data.licenseList.forEach(e => {
                                e.deadLine = this.$time(
                                    new Date(e.deadLine),
                                    'yyyy-MM-dd'
                                )
                            })
                            this.obj = {
                                ...data
                            }
                        } else {
                            this.$Message.error(res.message)
                        }
                    })
            } else {
                this.$api.companyOrganization.getEnterpriseDetail(this.$route.params.objectId)
                    .then(resp => {
                        if (resp.code == 200) {
                            if (resp.data.deadLine) {
                                resp.data.deadLine = this.$time(
                                    new Date(resp.data.deadLine),
                                    'yyyy-MM-dd'
                                )
                            }
                            this.obj = resp.data;
                        } else {
                            this.$Message.warning(resp.message)
                        }
                    })
            }
        }
    }
}
</script>
